﻿using Gobang.DataServer.Models;
using Gobang.ToolClass;
using Gobang.ToolClass.Model;
using System.Net;

namespace Gobang.Server.Servers.ServerTask
{
    internal class MsgTypeCommandTask
    {
        private readonly ServersCenter _serversCenter;
        public MsgTypeCommandTask(ServersCenter serversCenter)
        {
            _serversCenter = serversCenter;
        }
        internal void MsgTypeCommand(MsgMergeModel msgMergeModel, IPEndPoint iPEndPoint)
        {
            switch (msgMergeModel.SendMsgCommand)
            {
                case MsgCommand.Registering: // 注册
                    Registering(msgMergeModel, iPEndPoint);
                    break;
                case MsgCommand.Logining:
                    Logining(msgMergeModel, iPEndPoint);
                    break;

            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="msgMergeModel"></param>
        /// <param name="iPEndPoint"></param>
        private void Registering(MsgMergeModel msgMergeModel, IPEndPoint iPEndPoint)
        {
            Gb_UserInfo gb_UserInfo = _serversCenter.ServersMapper.Map<Gb_UserInfo>(msgMergeModel.UserInfo);
            bool status = false;
            string mes = string.Empty;
            try
            {
                status = _serversCenter._userServer.RegisterUser(gb_UserInfo);
            }
            catch (Exception e)
            {
                mes = e.Message;
            }
            if (status)
            {
                UserInfoModel user = _serversCenter.ServersMapper.Map<UserInfoModel>(gb_UserInfo);
                msgMergeModel.UserInfo = user;
                msgMergeModel.IsSuccess = true;
                msgMergeModel.SendMsgCommand = MsgCommand.Registered;
            }
            else
            {
                msgMergeModel.IsSuccess = false;
                msgMergeModel.Message = mes;
                msgMergeModel.SendMsgCommand = MsgCommand.RegisterAt;
            }
            var bytes = msgMergeModel.GetBytes();
            _serversCenter.Client.Send(iPEndPoint, bytes);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="msgMergeModel"></param>
        /// <param name="iPEndPoint"></param>
        private void Logining(MsgMergeModel msgMergeModel, IPEndPoint iPEndPoint)
        {
            string mes = string.Empty;
            Gb_UserInfo userInfo = null;
            try
            {
                userInfo = _serversCenter._userServer.Login(msgMergeModel.UserInfo.Accountnumber, msgMergeModel.UserInfo.Password);
            }
            catch (Exception e)
            {
                mes = e.Message;
            }
            if (userInfo != null)
            {
                UserInfoModel user = _serversCenter.ServersMapper.Map<UserInfoModel>(userInfo);
                msgMergeModel.UserInfo = user;
                msgMergeModel.IsSuccess = true;

                string areas = _serversCenter._areaCenter.AreaNodes.SerializeObject();
                msgMergeModel.DataStr = areas;
            }
            else
            {
                msgMergeModel.IsSuccess = false;
                msgMergeModel.Message = mes;
            }
            msgMergeModel.SendMsgCommand = MsgCommand.Loginend;
            var bytes = msgMergeModel.GetBytes();
            _serversCenter.Client.Send(iPEndPoint, bytes);
        }
    }
}
